package com.chima.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import com.chima.dao.PostCompetentDao;
import com.chima.domain.Employees;
import com.chima.domain.PageRequestBean;
import com.chima.domain.PageResponseBean;
import com.chima.domain.PostCompetent;
import com.chima.service.PostCompetentService;

public class PostCompetentServiceImp implements PostCompetentService {
	private PostCompetentDao postCompetentDao;
	
	public PostCompetentDao getPostCompetentDao() {
		return postCompetentDao;
	}

	public void setPostCompetentDao(PostCompetentDao postCompetentDao) {
		this.postCompetentDao = postCompetentDao;
	}

	public PageResponseBean findPostCompetent(PageRequestBean pageRequestBean) {
		int firstResult =(pageRequestBean.getPage()-1)*pageRequestBean.getLimit();
		int maxResults =pageRequestBean.getLimit();
		int total = postCompetentDao.findByTotalCount();
		// 获取分页查询条件
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(PostCompetent.class);
//		DetachedCriteria detachedCriteria = pageRequestBean.getCriteria();// 不一定存在
		List<PostCompetent> postCompetents = postCompetentDao.findByCriteria(detachedCriteria, firstResult, maxResults);
		PageResponseBean pageResponseBean = new PageResponseBean();
		pageResponseBean.setRows(postCompetents);
		pageResponseBean.setTotal(total);
		return pageResponseBean;
	}

	public void savePostCompetent(PostCompetent postCompetent) {
		postCompetentDao.save(postCompetent);
		
	}

	public void updatePostCompetent(PostCompetent postCompetent) {
		postCompetentDao.update(postCompetent);
		
	}

	public void deletePostCompetent(int id) {
		postCompetentDao.delete(id);
		
	}

	public PostCompetent findByGradeAndWorkyear(String grade, String workyear) {
		return  postCompetentDao.findByGradeAndWorkyaer(grade, workyear);
	}

//	public double findCoffer(Employees employee) {
//	   	double coffer=0.00;
//		 if(employee.getZhiban()==1){
//			   if( employee.getEducation().equals("专科")){
//
//				   coffer=(postCompetentDao.findByGradeAndWorkyaer(employee.getProfessional().getGrade(), employee.getWorkage()).getCoeff()+0.02+0.02); 
//			   }else if (employee.getEducation().equals("本科")) {
//				   
//				   coffer=(postCompetentDao.findByGradeAndWorkyaer(employee.getProfessional().getGrade(), employee.getWorkage()).getCoeff()+0.04+0.02);
//	
//			  }else if(employee.getEducation().equals("硕士")){
//				  
//				  coffer=(postCompetentDao.findByGradeAndWorkyaer(employee.getProfessional().getGrade(), employee.getWorkage()).getCoeff()+0.06+0.02);
//				  
//			  }else if(employee.getEducation().equals("博士")){
//				  
//				  coffer=(postCompetentDao.findByGradeAndWorkyaer(employee.getProfessional().getGrade(), employee.getWorkage()).getCoeff()+0.08+0.02);
//			  } else {
//				  
//				  coffer=(postCompetentDao.findByGradeAndWorkyaer(employee.getProfessional().getGrade(), employee.getWorkage()).getCoeff()+0.02);
//			 }
//		   }else{
//			   if( employee.getEducation().equals("专科")){
//				   coffer=(postCompetentDao.findByGradeAndWorkyaer(employee.getProfessional().getGrade(), employee.getWorkage()).getCoeff()+0.02); 
//		
//			   }else if (employee.getEducation().equals("本科")) {
//				   
//				   coffer=(postCompetentDao.findByGradeAndWorkyaer(employee.getProfessional().getGrade(), employee.getWorkage()).getCoeff()+0.04); 
//					
//			  }else if(employee.getEducation().equals("硕士")){
//				   coffer=(postCompetentDao.findByGradeAndWorkyaer(employee.getProfessional().getGrade(), employee.getWorkage()).getCoeff()+0.06); 
//				
//			  }else if(employee.getEducation().equals("博士")){
//				   coffer=(postCompetentDao.findByGradeAndWorkyaer(employee.getProfessional().getGrade(), employee.getWorkage()).getCoeff()+0.08); 
//				   
//			  } else {
//				  coffer=(postCompetentDao.findByGradeAndWorkyaer(employee.getProfessional().getGrade(), employee.getWorkage()).getCoeff()); 
//			  }
//		   }
//		   return coffer;
//	 }
	
	public double findCoffer (Employees e) { 
		System.out.println(e.getName()+"============================");
	   	 double coffer=0.00;
	   	String workyear=null;
		try {
			workyear = FindYear(e.getWorkage());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		 if(e.getEducation().equals("专科") || e.getEducation().equals("本科") ||e.getEducation().equals("硕士") ||e.getEducation().equals("博士")){
			
			 coffer=postCompetentDao.findPostCompetent(e.getProfessional().getGrade(),workyear, e.getEducation(),e.getZhiban()).getCoeff();
			 
		   }else{
			   
			   coffer=postCompetentDao.findPostCompetent(e.getProfessional().getGrade(), workyear, "其他",e.getZhiban()).getCoeff();
		   }
		   return coffer;
	 }
	public String  FindYear(String  rzdate) throws Exception {
		   
		    String time = null;
		    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		    String nowdate =sdf.format(new Date());
		    int  between_days=s(nowdate,rzdate)/365;
	        if(between_days<6 && between_days>=0){
	        	time="0-5年";
	        }else if(between_days<11 && between_days>5){
	        	time="6-10年";
	        }else if(between_days<16 && between_days>10){
	        	time="11-15年";	
	        }else if(between_days<21 && between_days>15){
	        	time="16-20年";
	        }else if(between_days<26 && between_days>20){
	        	time="21-25年";
	        }else if(between_days<31 && between_days>25){
	        	time="26-30年";
	        }else if(between_days<36 && between_days>30){
	        	time="31-35年";
	        }else if(between_days<41 && between_days>35){
	        	time="36-40年";
	        }else if(between_days>40){
	        	time="40年以上";
	        }
	        
		return time;
		
	}
	
	private static int s (String a , String b) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date ndate = sdf.parse(a);
		Date rdate = sdf.parse(b);
		Calendar aa = Calendar.getInstance();
		Calendar bb = Calendar.getInstance();
		aa.setTime(ndate);
		bb.setTime(rdate);
		setTimeMidNight(aa);
		setTimeMidNight(bb);
		long aaMs = aa.getTimeInMillis();
		long bbMs = bb.getTimeInMillis();
		long ab = aaMs - bbMs;
		return millsecondTodays(ab);
	}
	private static int millsecondTodays(long s){
		return (int)(s/(1000*86400));
	}
	private static void setTimeMidNight(Calendar c){
		c.set(c.HOUR_OF_DAY,0);
		c.set(c.MINUTE,0);
		c.set(c.SECOND, 0);
	}
}
